package memory

import (
	"booking-service/internal/models"
	"testing"
	"time"
)

func TestGetAndSetAvailability(t *testing.T) {
	storage := NewInMemoryRoomAvailabilityStorage()

	date := time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)
	availability := models.RoomAvailability{
		HotelID: "reddison",
		RoomID:  "lux",
		Date:    date,
		Quota:   1,
	}

	err := storage.UpdateAvailability("reddison", "lux", date, 1)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	avail, err := storage.GetAvailability("reddison", "lux", date)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	if avail != availability {
		t.Errorf("expected %v, got %v", availability, avail)
	}

	err = storage.UpdateAvailability("reddison", "lux", date, 0)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	avail, err = storage.GetAvailability("reddison", "lux", date)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	if avail.Quota != 0 {
		t.Errorf("expected quota to be 0, got %d", avail.Quota)
	}
}

func TestGetAvailabilityNotExist(t *testing.T) {
	storage := NewInMemoryRoomAvailabilityStorage()

	date := time.Date(2024, 1, 5, 0, 0, 0, 0, time.UTC)
	_, err := storage.GetAvailability("reddison", "lux", date)
	if err == nil {
		t.Errorf("expected error, got nil")
	}
}
